﻿// ********************************************
// ClassName  ：  BluetoothService.cs
// Author        ：
// Company     ： 智测心控
// Version        ：V1.0
// CreateTime ： {date}
// Description ：
// ********************************************

using Common.Common;
using DBLibrary;
using DcpServer.Common;
using DcpServer.Domain;
using DcpServer.Domain.Dto;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using WSDTO;
using WSLibrary;

namespace WCFLibrary
{
    public class BluetoothService : IBluetoothService
    {

        public static String lockStr = "lock";
        
        public void SendData(string sendData,Guid patientID,String deviceID)
        {
            //Console.Write(sendData);
           VitalSignData vitalSignData = JsonConvert.DeserializeObject<VitalSignData>(sendData);

            // 维护在线的病人信息
            if(WebContext.OnlinePatient == null)
            {
                WebContext.OnlinePatient = new Dictionary<string, Patient>();
            }

            Patient patient;
            lock (lockStr)
            {
                if (!WebContext.OnlinePatient.ContainsKey(deviceID))
                {
                    // 将病人信息放入Online设备维护
                    // 从数据库获取病人信息
                    patient = PatientRepository.SelectPatient(patientID);
                    if (patient == null)
                    {
                        LogHelper.Info("病人不存在，请添加病人！");
                        return;
                    }

                    WebContext.OnlinePatient.Add(deviceID, patient);
                }
                else if (patientID != WebContext.OnlinePatient[deviceID].Id)
                {
                    // 更新在线的信息
                    patient = PatientRepository.SelectPatient(patientID);
                    WebContext.OnlinePatient[deviceID] = patient;
                    LogHelper.Info(deviceID + "新病人上线: " + patient.PatientName);
                }
            }
            
            if (vitalSignData != null)
            {
                // 将该类转换为和JAVA客户端通信的DTO 并将数据进行持久化
                VitalSignDataDTO vitalSignDataDTO = DtoTransForm(vitalSignData,patientID);

                vitalSignDataDTO.DeviceId = deviceID;
                vitalSignDataDTO.PatientID = patientID.ToString();
                String WSDTOStr = JsonConvert.SerializeObject(vitalSignDataDTO) + "#_#";
                LogHelper.Info("收到设备：" + deviceID + " 病人：" + WebContext.OnlinePatient[deviceID].PatientName + "的数据");
                // 使用Websocket转发
                WSConfig.Relay(WSDTOStr);
            }
           
        }

        public void AddPatient(string patientInfo)
        {
            PatientRepository patientRepository = new PatientRepository();
            Patient patient = JsonConvert.DeserializeObject<Patient>(patientInfo);
            patientRepository.AddPatient(patient);
        }


        public VitalSignDataDTO DtoTransForm(VitalSignData vitalSignData,Guid patientId)
        {
            
            VitalSignDataDTO vitalSignDataDTO = new VitalSignDataDTO()
            {
                SerialNo = vitalSignData.SerialNo,
                //SendTime = vitalSignData.SendTime,
            };
            
            if(vitalSignData.ValueData != null)
            {
                ValueData valueData = vitalSignData.ValueData;
                vitalSignDataDTO.ValueData = new ValueDataDTO()
                {
                    SerialNo = valueData.SerialNo,
                    Hr = valueData.Hr,
                    Resp = valueData.Resp,
                    Spo2 = valueData.Spo2,
                    Temp = valueData.Temp,
                    Power = valueData.Power
                };

                // 数据存库
                PatientRepository repository = new PatientRepository();
                repository.AddValueData(valueData,patientId);
                //LogHelper.Info("收到值数据");

            }
            else if(vitalSignData.WaveData != null)
            {
                WaveData waveData = vitalSignData.WaveData;
                vitalSignDataDTO.WaveData = new WaveDataDTO()
                {
                    SerialNo = waveData.SerialNo,
                    Ecg = waveData.Ecg,
                    Spo2 = new ushort[125],
                    Resp = new ushort[250]
                };
                for(int i = 0; i < 125; i++)
                {
                    vitalSignDataDTO.WaveData.Spo2[i] = waveData.Spo2[i];
                }

                for (int i = 0; i < 250; i++)
                {
                    vitalSignDataDTO.WaveData.Resp[i] = waveData.Resp[i];
                }
                //LogHelper.Info("收到波形数据");

            }

            vitalSignDataDTO.NibpReview = vitalSignData.NibpReview;
            // NIBP记录存库
            if (vitalSignData.NibpReview != null && vitalSignData.NibpReview.Ns != null) {
                var patientrepository = new PatientRepository();
                patientrepository.UpdateMainDataTableNIBP(vitalSignData.NibpReview, vitalSignData.NibpReview.PatientId);
                LogHelper.Info("收到血压数据");
            }
            
            return vitalSignDataDTO;
        }
    
        public void Archive(Guid patientId)
        {
            var patientrepository = new PatientRepository();
            patientrepository.Archive(patientId);
        }
    }
}